<template>
  <div>
    <table
      border="1"
      width="700"
      style="border-collapse: collapse"
    >
      <caption>
        购物车
      </caption>
      <thead>
        <tr>
          <th>
            <input type="checkbox" :checked="isAllChecked"  @change="changeFn"/> <span>全选</span>
          </th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>总价</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <Buy v-for="(arr,index) in goodList" :key="index"  
        :obj="arr"
        :index="index" 
        @jian-todo="babaJianFn"
        @jia-todo="babaJiaFn"
        @del-todo="babaDelFn"
        />
      </tbody>
      <tfoot>
        <tr>
          <td>合计:{{count}}</td>
          <td colspan="5">
            
          </td>
        </tr>
      </tfoot>
    </table>
  </div>
</template>

<script>
import Buy from "./components/Buy.vue"
export default {
    computed:{
isAllChecked(){
    return  this.goodList.every(item =>item.checked)
},
count(){
    return this.goodList.reduce((accu,val)=>{
        if(val.checked===true){
            return  accu+=val.price*val.num
        }
        return accu
    },0)
}
    },
    methods:{
babaJianFn(i){
    this.goodList[i].num--
},
babaJiaFn(i){
this.goodList[i].num++
},
babaDelFn(i){
this.goodList.splice(i,1)
},
changeFn(e){
this.goodList.forEach(item => item.checked=e.target.checked)
}
    },
    components:{
        Buy
    },
  data() {
    return {
      goodList: [
        {
          name: "诸葛亮",
          price: 1000,
          num: 1,
          checked: false,
        },
        {
          name: "蔡文姬",
          price: 1500,
          num: 1,
          checked: false,
        },
        {
          name: "妲己",
          price: 2000,
          num: 1,
          checked: false,
        },
        {
          name: "鲁班",
          price: 2200,
          num: 1,
          checked: false,
        },
      ],
    };
  },
};
</script>

<style>
</style>